package com.xxwy.lifecycle;

import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationEventPublisher;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.core.io.ResourceLoader;

/**
 * 依赖来源.
 *
 * @author xxwy
 * @date 2021/7/13 0013
 */
public class DependencySourceDemo {

    /**
     * 依赖注入：postProcessProperties 早于setter 早于 @PostConstruct
     */
    @Autowired
    private BeanFactory beanFactory;
    @Autowired
    private ResourceLoader resourceLoader;
    @Autowired
    private ApplicationContext applicationContext;
    @Autowired
    private ApplicationEventPublisher applicationEventPublisher;

    /**
     * @Link org.springframework.context.support.AbstractApplicationContext#prepareBeanFactory(org.springframework.beans.factory.config.ConfigurableListableBeanFactory)
     * @param args
     */
    public static void main(String[] args) {
        AnnotationConfigApplicationContext applicationContext = new AnnotationConfigApplicationContext();
        applicationContext.register(DependencySourceDemo.class);
        applicationContext.refresh();

        // 依赖查找
        DependencySourceDemo bean = applicationContext.getBean(DependencySourceDemo.class);
        System.out.println(bean);

        // 依赖注入, 就一样的，具体查看源码
        System.out.println(bean.beanFactory);
        System.out.println(bean.applicationContext.getAutowireCapableBeanFactory());
        System.out.println(bean.applicationContext != bean.applicationEventPublisher);
        System.out.println(bean.applicationEventPublisher);
        System.out.println(bean.resourceLoader);
        applicationContext.close();
    }
}
